﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class MVTVKEK : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        MVTVKEKData m_Data, m_DataX;
        public DataSet setUI=new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public INTEXDBC INTEXDBC;
        private DataRow dwUI;
        DataTable tableUI;
        private string fromshippingdate = "", endshippingdate = "";
        private string fromdndate = "", enddndate = "";
        private string fromekdate = "", endekdate = "";   
        #endregion
        public MVTVKEK()
        {
            InitializeComponent();
            this.Name = "订单交期";
           
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new MVTVKEKData();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("ARTICLE", typeof(string));
            tableUI.Columns.Add("PI", typeof(string));
            tableUI.Columns.Add("SUP", typeof(string));
            tableUI.Columns.Add("KUNDNR", typeof(string));
            tableUI.Columns.Add("NEUBEN", typeof(string));
            tableUI.Columns.Add("SACHBE", typeof(string));
            tableUI.Columns.Add("FROMDATE", typeof(string));
            tableUI.Columns.Add("ENDDATE", typeof(string));
            tableUI.Columns.Add("FROMDNDATE", typeof(string));
            tableUI.Columns.Add("ENDDNDATE", typeof(string));
            tableUI.Columns.Add("FROMEKDATE", typeof(string));
            tableUI.Columns.Add("ENDEKDATE", typeof(string));
        }

        private void InitDataBinding()
        {
             
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow(); 
                InitDataBinding();
                txtKUNDNR.Focus();
                /*
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT Distinct B.LSNAME1,A.ABPAUFKRS ,A.ABPAUFLFD,A.ABPAUFPOS,A.ABPART,A.ABPVAR, "
                    + "　A.ABPFARBE,A.ABPSOLLMENGE1/100 AS WEI, A.ABPSTARTDAT,A.ABPENDDAT,'' AS REMARK, "
                    + " C.ABADIAUFKRS||'-'||TO_NUMBER(C.ABADIAUFLFD)||'-'||TO_NUMBER(C.ABADIAUFPOS) AS DIORDER "
                    + " FROM ABPOS A,LIEFDAT B,ABAUFM C "
                    + " WHERE A.ABPMANDANT = B.LSMANDANT AND A.ABPLIEFNR = B.LSLIEFNR "
                    + " AND A.ABPMANDANT = C.ABAMANDANT AND A.ABPAUFKRS = C.ABAAUFKRS "
                    + " AND A.ABPAUFLFD = C.ABAAUFLFD AND A.ABPAUFPOS = C.ABAAUFPOS "
                    + " AND A.ABPAUFART='PR'  AND A.ABPERLKZ <>'J' AND C.ABAAPOS='0001' "
                 ;
                */
                /*DataSet setResult = new DataSet();
                setResult = m_Data.LoadData(); 
                setUI.Merge(setResult);  
                this.DataContext = setUI;*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
            return;

        }
        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet setResult = new DataSet();            
                setUI.Clear();
                setUI.AcceptChanges();       
                string ARTICLE = txtARTICLE.Text.Trim();
                string SUP = txtSUP.Text.Trim(); 
                string KUNDNR = txtKUNDNR.Text.Trim(); 
                string NEUBEN = txtNEUBEN.Text.Trim();
                string SACHBE = txtSACHBE.Text.Trim();
                string PI = txtPI.Text.Trim();
                
                if (ARTICLE == "")
                {
                    ARTICLE = "AAA";
                }
                if(SUP == "")
                {
                    SUP = "AAA";
                }
                if (KUNDNR == "")
                {
                    KUNDNR = "AAA";
                }
                if (NEUBEN == "")
                {
                    NEUBEN = "AAA";
                }
                if (SACHBE == "")
                {
                    SACHBE = "AAA";
                }
                if (PI == "")
                {
                    PI = "AAA";
                }
                if(fromshippingdate == "")
                {
                    fromshippingdate = "20000000";
                }
                if (endshippingdate == "")
                {
                    endshippingdate = "99999999";
                }
                if (fromdndate == "")
                {
                    fromdndate = "20000000";
                }
                if (enddndate == "")
                {
                    enddndate = "99999999";
                }
                if (fromekdate == "")
                {
                    fromekdate = "20000000";
                }
                if (endekdate == "")
                {
                    endekdate = "99999999";
                }


                dwUI["ARTICLE"] = ARTICLE;
                dwUI["SUP"] = SUP;
                dwUI["PI"] = PI;
                dwUI["KUNDNR"] = KUNDNR;
                dwUI["NEUBEN"] = NEUBEN;
                dwUI["SACHBE"] = SACHBE;
                dwUI["FROMDATE"] = fromshippingdate;
                dwUI["ENDDATE"] = endshippingdate;
                dwUI["FROMDNDATE"] = fromdndate;
                dwUI["ENDDNDATE"] = enddndate;
                dwUI["FROMEKDATE"] = fromekdate;
                dwUI["ENDEKDATE"] = endekdate;
                setResult = m_Data.BuildMVTVKEKData(dwUI);
                setUI.Merge(setResult);
                MasterDg.DataContext = setUI.Tables["VKEK"];
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }       
        private void btnEXCEL_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new MVTVKEKData(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            MVTVKEKReport m_rpt = new MVTVKEKReport(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL");
        }
        private void FROM_SHIPPING_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromshippingdate = "";
                fromshippingdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void END_SHIPPING_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                endshippingdate = "";
                endshippingdate = m_Data.GetDate(sender);
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            btnQuery_Click(null,null);

        }
        private void FROM_DN_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromdndate = "";
                fromdndate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void END_DN_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                enddndate = "";
                enddndate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void DataGridTextColumn_Selected(object sender, RoutedEventArgs e)
        {

        }

        private void FROM_EK_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                fromekdate = "";
                fromekdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void END_EK_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                endekdate = "";
                endekdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

    }
}
